package com.avic.rong.search.fragment;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.search.activity.SearchResaultActivity;
import com.avic.rong.search.adapter.SearchResultVpAdapter;
import com.avic.rong.search.fragment.searchResultFragments.SearchArticleFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchBiaoZhunFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchGoodsFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchPeiXunFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchQuanBuFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchResultFragment extends BaseFragment {

    @BindView(R.id.search_TabLayout)
    TabLayout searchTabLayout;
    @BindView(R.id.search_Vp)
    ViewPager searchVp;
    @BindView(R.id.search_shaixuan_tv)
    TextView searchShaixuanTv;
    @BindView(R.id.search_Result_ShaiXuan_Img)
    ImageView searchResultShaiXuanImg;
    private View view;
    private List<String> titleList = new ArrayList<>();
    private List<Fragment> fragmentList = new ArrayList<>();
    private SearchResultVpAdapter vpAdapter;

    public SearchResultFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_search_result, container, false);
        ButterKnife.bind(this, view);
        searchTabLayout.setupWithViewPager(searchVp);
        initData();
        addListener();
        return view;
    }

    protected void addListener() {
        searchTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getText().equals("全部") || tab.getText().equals("文章") || tab.getText().equals("培训")) {
                    searchShaixuanTv.setVisibility(View.GONE);
                    searchResultShaiXuanImg.setVisibility(View.GONE);
                } else {
                    searchShaixuanTv.setVisibility(View.VISIBLE);
                    searchResultShaiXuanImg.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        searchShaixuanTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchTabLayout.getSelectedTabPosition() == 1) {
                    SearchResaultActivity activity = (SearchResaultActivity) getActivity();
                    activity.openBzDrawer();
                } else if (searchTabLayout.getSelectedTabPosition() == 2) {
                    SearchResaultActivity activity = (SearchResaultActivity) getActivity();
                    activity.openGoodsDrawer();
                }
            }
        });
        searchResultShaiXuanImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchTabLayout.getSelectedTabPosition() == 1) {
                    SearchResaultActivity activity = (SearchResaultActivity) getActivity();
                    activity.openBzDrawer();
                } else if (searchTabLayout.getSelectedTabPosition() == 2) {
                    SearchResaultActivity activity = (SearchResaultActivity) getActivity();
                    activity.openGoodsDrawer();
                }
            }
        });
    }

    @Override
    protected int getContentViewId() {
        return 0;
    }

    private void initData() {
        titleList.add("全部");
        titleList.add("标准");
        titleList.add("培训");
        titleList.add("商品");
        titleList.add("文章");
        fragmentList.add(new SearchQuanBuFragment());
        fragmentList.add(new SearchBiaoZhunFragment());
        fragmentList.add(new SearchPeiXunFragment());
        fragmentList.add(new SearchGoodsFragment());
        fragmentList.add(new SearchArticleFragment());
        vpAdapter = new SearchResultVpAdapter(getChildFragmentManager(), titleList, fragmentList);
        searchVp.setAdapter(vpAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected void initView() {

    }
}
